using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

using LibrErian.Model;
using Microsoft.Practices.CompositeUI.WinForms;
using System.Collections;

namespace LibrErian.Modules.BasicBook
{
    [SmartPart]
    public partial class BookView : UserControl, IBookView 
    {
        private Book currentBook;
        private BookViewPresenter presenter;

        [CreateNew]
        public BookViewPresenter Presenter
        {
            set 
            {
                presenter = value;
                presenter.View = this;
            }
        }

        public void DisplayImage(Image image)
        {
            pbImage.Image = image;
            btnAddImage.Visible = false;
        }

        public void ShowMessage(string message)
        {
            lblMessage.Text = message;
            pnlMessage.Visible = true;
        }

        public void SetBook(Book book)
        {
            currentBook = book;
            bookBindingSource.DataSource = book;
        }

        public void SetFormats(IList formats)
        {
            cmbFormat.DataSource = formats;
        }

        public void SetGenres(IList genres)
        {
            clbGenre.DataSource = genres;

            for (int i = 0; i < clbGenre.Items.Count; i++)
            {
                if (currentBook.Genres.Contains(clbGenre.Items[i]))
                {
                    clbGenre.SetItemChecked(i, true);
                }
            }
        }

        public void ResetDataSource()
        {
            bookBindingSource.ResetCurrentItem();
        }

        public BookView()
        {
            InitializeComponent();
        }

        private void BookView_Load(object sender, EventArgs e)
        {
            presenter.OnViewReady();
        }

        private void btnRemoveAuthor_Click(object sender, EventArgs e)
        {
            Author selectedAuthor = lbAuthor.SelectedItem as Author;
            presenter.OnRemoveAuthor(selectedAuthor);
        }

        private void btnAddAuthor_Click(object sender, EventArgs e)
        {
            presenter.OnSelectAuthor();
        }

        private void pbImage_Click(object sender, EventArgs e)
        {
            presenter.OnImage();
        }

        private void btnAddImage_Click(object sender, EventArgs e)
        {
            presenter.OnImage();
        }

        private bool IsValid()
        {
            if (string.IsNullOrEmpty(tbTitle.Text))
            {
                epValidation.SetError(tbTitle, "The title cannot be empty");
                return false;
            }
            return true;
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            if (IsValid())
            {
                Book currentBook = bookBindingSource.Current as Book;

                foreach(Genre genre in clbGenre.CheckedItems)
                {
                    if (!currentBook.Genres.Contains(genre))
                    {
                        currentBook.Genres.Add(genre);
                    }
                }

                foreach (Genre genre in currentBook.Genres)
                {
                    if (!clbGenre.CheckedItems.Contains(genre))
                    {
                        currentBook.Genres.Remove(genre);
                    }
                }

                bookBindingSource.EndEdit();
                presenter.OnSave();
            }
        }

        private void tsbCancel_Click(object sender, EventArgs e)
        {
            presenter.OnCancel();
        }

        private void btnManageGenre_Click(object sender, EventArgs e)
        {
            presenter.OnManageGenre();
        }
    }
}
